<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="" prop="vehicleName">
                <el-input
                    v-model="queryParams.vehicleName"
                    placeholder="请输入车辆名称"
                    clearable
                    size="small"
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="" prop="licensePlate">
                <el-input
                    v-model="queryParams.licensePlate"
                    placeholder="请输入牌照"
                    clearable
                    size="small"
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="" prop="vehicleModel">
                <el-select v-model="queryParams.vehicleModel" placeholder="请选择车辆型号" clearable size="small">
                    <el-option
                        v-for="item in vehicleModelList"
                        :key="item.id"
                        :label="item.modelName"
                        :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="" prop="vehicleStatus">
                <el-select v-model="queryParams.vehicleStatus" placeholder="请选择车辆状态" clearable size="small">
                    <el-option v-for="item in vehicleStatusList" :key="item.key" :label="item.value" :value="item.key"/>
                </el-select>
            </el-form-item>
            <el-form-item label="" prop="createDate">
                <!--<el-input
                    v-model="queryParams.createDate"
                    placeholder="请输入创建时间"
                    clearable
                    size="small"
                    @keyup.enter.native="handleQuery"
                />-->
                <el-date-picker
                    v-model="queryParams.createDate"
                    type="daterange"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    value-format="timestamp">
                </el-date-picker>
            </el-form-item>
            <el-form-item>
                <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                    type="primary"
                    icon="el-icon-plus"
                    size="mini"
                    @click="handleAdd"
                    v-hasPermi="['system:vehicle:add']"
                >新增
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                    type="success"
                    icon="el-icon-edit"
                    size="mini"
                    :disabled="single"
                    @click="handleUpdate"
                    v-hasPermi="['system:vehicle:edit']"
                >修改
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                    type="danger"
                    icon="el-icon-delete"
                    size="mini"
                    :disabled="multiple"
                    @click="handleDelete"
                    v-hasPermi="['system:vehicle:remove']"
                >删除
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                    type="warning"
                    icon="el-icon-download"
                    size="mini"
                    @click="handleExport"
                    v-hasPermi="['system:vehicle:export']"
                >导出
                </el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="vehicleList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center"/>
            <el-table-column label="编号" align="center" prop="id"/>
            <el-table-column label="车辆名称" align="center" prop="vehicleName"/>
            <el-table-column label="牌照" align="center" prop="licensePlate"/>
            <el-table-column label="车型" align="center" prop="modelName"/>
            <el-table-column label="载客量" align="center" prop="num"/>
            <el-table-column label="车辆状态" align="center" prop="vehicleStatus">
                <template slot-scope="scope">
                    <span>{{scope.row.vehicleStatus === 0? "待发车":"已发车"}}</span>
                </template>
            </el-table-column>
            <el-table-column label="创建时间" align="center" prop="createDate">
                <template slot-scope="scope">
                    <span>{{parseTime(scope.row.createDate)}}</span>
                </template>
            </el-table-column>
            <el-table-column label="创建者" align="center" prop="createUserName"/>
            <el-table-column label="更新时间" align="center" prop="updateDate"/>
            <el-table-column label="更新者" align="center" prop="updateUserName"/>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-edit"
                        @click="handleUpdate(scope.row)"
                        v-hasPermi="['system:vehicle:edit']"
                    >修改
                    </el-button>
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-delete"
                        @click="handleDelete(scope.row)"
                        v-hasPermi="['system:vehicle:remove']"
                    >删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        />

        <!-- 添加或修改对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="车辆名称" prop="vehicleName">
                    <el-input v-model="form.vehicleName" placeholder="请输入车辆名称"/>
                </el-form-item>
                <el-form-item label="牌照" prop="licensePlate">
                    <el-input v-model="form.licensePlate" placeholder="请输入牌照"/>
                </el-form-item>
                <el-form-item label="车辆型号" prop="vehicleModel">
                    <el-select v-model="form.vehicleModel" clearable placeholder="请选择车型" style="width: 380px">
                        <el-option
                            v-for="item in vehicleModelList"
                            :key="item.id"
                            :label="item.modelName"
                            :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="车辆状态" prop="vehicleStatus">
                    <el-select v-model="form.vehicleStatus" clearable style="width: 380px">
                        <el-option v-for="item in vehicleStatusList" :key="item.key" :label="item.value" :value="item.key"/>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import {
        listVehicle,
        getVehicle,
        delVehicle,
        addVehicle,
        updateVehicle,
        exportVehicle
    } from "../../../api/backstage/vehicle";
    import {listModel} from "../../../api/backstage/vehicleModel"

    export default {
        name: "Vehicle",
        components: {},
        data() {
            return {
                // 遮罩层
                loading: true,
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 【请填写功能名称】表格数据
                vehicleList: [],
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    vehicleName: null,
                    licensePlate: null,
                    vehicleModel: null,
                    vehicleStatus: null,
                    createDate: null,
                },
                // 表单参数
                form: {
                    vehicleStatus: 0
                },
                vehicleStatusList: [
                    {key: 0,value: "待发车"},
                    {key: 1, value: "已发车"}
                ],
                vehicleModelList: [],
                // 表单校验
                rules: {
                    vehicleName: [
                        {required: true, message: "车辆名称不能为空", trigger: "blur"}
                    ],
                    licensePlate: [
                        {required: true, message: "牌照不能为空", trigger: "blur"}
                    ],
                    vehicleModel: [
                        {required: true, message: "车型不能为空", trigger: "blur"}
                    ],
                    vehicleStatus: [
                        {required: true, message: "车辆状态不能为空", trigger: "blur"}
                    ],
                }
            };
        },
        created() {
            this.getList();
            this.getVehicleModelList();
        },
        methods: {
            getVehicleModelList() {
                listModel().then(resp => {
                    this.vehicleModelList = resp.rows;
                })
            },
            /** 查询【请填写功能名称】列表 */
            getList() {
                this.loading = true;
                listVehicle(this.queryParams).then(response => {
                    this.vehicleList = response.rows;
                    this.total = response.total;
                    this.loading = false;
                });
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                    id: null,
                    vehicleName: null,
                    licensePlate: null,
                    vehicleModel: null,
                    vehicleStatus: null,
                    isDel: null,
                    isLock: null,
                    version: null,
                    createDate: null,
                    createUser: null,
                    updateDate: null,
                    updateUser: null
                };
                this.resetForm("form");
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm("queryForm");
                this.handleQuery();
            },
            // 多选框选中数据
            handleSelectionChange(selection) {
                this.ids = selection.map(item => item.id)
                this.single = selection.length !== 1
                this.multiple = !selection.length
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.reset();
                this.open = true;
                this.title = "添加";
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset();
                const id = row.id || this.ids
                this.loading = true;
                getVehicle(id).then(response => {
                    this.form = response.data;
                    this.open = true;
                    this.title = "修改";
                    this.loading = false;
                });
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        if (this.form.id != null) {
                            updateVehicle(this.form).then(response => {
                                this.msgSuccess("修改成功");
                                this.open = false;
                                this.getList();
                            });
                        } else {
                            addVehicle(this.form).then(response => {
                                this.msgSuccess("新增成功");
                                this.open = false;
                                this.getList();
                            });
                        }
                    }
                });
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                const ids = row.id || this.ids;
                this.$confirm('是否确认删除【请填写功能名称】编号为"' + ids + '"的数据项?', "警告", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(function () {
                    return delVehicle(ids);
                }).then(() => {
                    this.getList();
                    this.msgSuccess("删除成功");
                })
            },
            /** 导出按钮操作 */
            handleExport() {
                const queryParams = this.queryParams;
                this.$confirm('是否确认导出所有【请填写功能名称】数据项?', "警告", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(function () {
                    return exportVehicle(queryParams);
                }).then(response => {
                    this.download(response.msg);
                })
            }
        },
    };
</script>
